package com.tyut.homework.service.impl;

import com.tyut.homework.pojo.FinanceHandle;
import com.tyut.homework.repository.FinanceHandleRepository;
import com.tyut.homework.service.FinanceService;
import org.hibernate.event.spi.SaveOrUpdateEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FinanceServiceImpl implements FinanceService {

    @Autowired
    private FinanceHandleRepository financeRepository;
    @Override
    public FinanceHandle save(FinanceHandle financeHandle) {
        return financeRepository.save(financeHandle);
    }

    @Override
    public int delete(Integer id) {
        FinanceHandle financeHandle = new FinanceHandle();
        financeHandle.setAptId(id);
        financeRepository.delete(financeHandle);
        return 1;
    }

    @Override
    public int update(FinanceHandle financeHandle) {
        financeRepository.saveAndFlush(financeHandle);
        return 1;
    }

    @Override
    public FinanceHandle find(Integer id) {
        return financeRepository.findById(id).get();
    }

    @Override
    public List<FinanceHandle> findAll() {
        return null;
    }

    @Override
    public Page<FinanceHandle> getPage(Integer pageNum, Integer pageLimit) {
        Sort sort = new Sort(Sort.Direction.ASC,"aptId");
        Pageable pageable = new PageRequest(pageNum, pageLimit, sort);
        return financeRepository.findAll(pageable);
    }
}
